package com.it.you.redis;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.util.Scanner;

public class RedisKeySerialize<K,V> {

    static {
        System.out.println("==========REDIS序列化key工具==========");
    }


    public String scan() {
        System.out.println("\n"+"* 请输入Key:");
        return new Scanner(System.in).next();
    }

    //构造一个序列化器
    public RedisSerializer keySerializer() {
        return new JdkSerializationRedisSerializer(this.getClass().getClassLoader());
    }

    //序列化key
    public byte[] rawKey(K key) {
        if (keySerializer() == null && key instanceof byte[]) {
            return (byte[])key;
        }
        return keySerializer().serialize(key);
    }

    public String handleKey(K key) {
        String returnStr = "";
        final byte[] bytes = rawKey(key);
        returnStr = new String(bytes);
        return returnStr;
    }

    public static void main(String[] args) {
        RedisKeySerialize keySerialize = new RedisKeySerialize<>();
        String key = keySerialize.scan();
        String result = keySerialize.handleKey(key);
        System.out.println("* 存于Redis中的key:" + result);
    }
}
